package transactionimplementation

import (
	Database "salarypayment-case/DataBase"
	"salarypayment-case/Model"
	"time"
)

type PaydayTransaction struct {
	itsPayDate   time.Time
	itsPaychecks map[int]Model.Paycheck
}

func (ps *PaydayTransaction) Execute() {
	empids := Database.PayrollDatabases.GetAllEmployeeIds()

	for _, empid := range empids {
		e := Database.PayrollDatabases.GetEmployee(empid)

		if e != nil {
			if e.IsPayDate(ps.itsPayDate) {
				pc := Model.CreatePaycheck(e.GetPayPeriodStartDate(ps.itsPayDate), ps.itsPayDate)
				ps.itsPaychecks[empid] = *pc
				e.Payday(*pc)
			}
		}
	}
}

func (ps *PaydayTransaction) GetPaycheck(empid int) Model.Paycheck {
	if p, ok := ps.itsPaychecks[empid]; ok {
		return p
	}

	return Model.Paycheck{}
}

func (ps *PaydayTransaction) GetPaycheckCount() int {
	return len(ps.itsPaychecks)
}
